/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.model.entity.provider;

// Generated Dec 6, 2009 5:47:57 PM by Hibernate Tools 3.2.2.GA
// powered by MAGS

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.mags.dao.provider.CacheableDataProvider;
import org.mags.remas.model.entity.EntityStructure;
import org.mags.remas.model.entity.EntityStructureId;

public class EntityStructureProvider extends CacheableDataProvider<EntityStructure> {
	private static final long serialVersionUID = 1L;
	private EntityTypeProvider entityTypeProvider;
	private ViewTypeProvider viewTypeProvider;
	
	public ViewTypeProvider getViewTypeProvider() {
		return viewTypeProvider;
	}
	public void setViewTypeProvider(ViewTypeProvider viewTypeProvider) {
		this.viewTypeProvider = viewTypeProvider;
	}
	public EntityStructureProvider(EntityTypeProvider entityTypeProvider, ViewTypeProvider viewTypeProvider) {
		this.entityTypeProvider = entityTypeProvider;
		this.viewTypeProvider = viewTypeProvider;
	}
	public EntityTypeProvider getEntityTypeProvider() {
		return entityTypeProvider;
	}
	public void setEntityTypeProvider(EntityTypeProvider entityTypeProvider) {
		this.entityTypeProvider = entityTypeProvider;
	}
	public boolean existsWithParentId(int parentId, short entityCategoryId) {
		for(EntityStructure i: this.getAll()) 
			if(parentId==i.getId().getEntityTypeParentId()&&entityCategoryId==i.getEntityType().getEntityCategory().getEntityCategoryId())
				return true;
		return false;
	}
	public List<EntityStructure> getByCategoryId(short entityCategoryId) {
		List<EntityStructure> many = new ArrayList<EntityStructure>();
		for(EntityStructure i: this.getAll())
			if(entityCategoryId==i.getEntityType().getEntityCategory().getEntityCategoryId())				
				many.add(i);
		return many;		
	}
	public List<EntityStructure> getByParentId(int entityTypeParentId, short entityCategoryId) {
		List<EntityStructure> many = new ArrayList<EntityStructure>();
		for(EntityStructure i: this.getAll())
			if(entityTypeParentId == i.getId().getEntityTypeParentId()&&entityCategoryId==i.getEntityType().getEntityCategory().getEntityCategoryId())				
				many.add(i);
		return many;
	}
	public EntityStructure getById(int entityTypeId, int entityTypeParentId) {		
		for(EntityStructure i: this.getAll())
			if(entityTypeId == i.getId().getEntityTypeId() 
			  && entityTypeParentId == i.getId().getEntityTypeParentId())
				return i;
		return null;
	}
	public EntityStructure getById(Serializable id) {
		for(EntityStructure i: this.getAll())
			if(i.getId().getEntityTypeId()==((EntityStructureId)id).getEntityTypeId() &&
			   i.getId().getEntityTypeParentId()==((EntityStructureId)id).getEntityTypeParentId())
				return i;
		return null;
	}
	protected void loadProperties(EntityStructure one) {
		one.setViewType(viewTypeProvider.getById(one.getViewType().getViewTypeId()));
		one.setEntityType(entityTypeProvider.getById(one.getId().getEntityTypeId()));
		one.setEntityTypeParent(entityTypeProvider.getById(one.getId().getEntityTypeParentId()));		
	}	
}
